#pragma once
#include "afxwin.h"
#include "Trajectory.h"
#include "GridParam.h"
#include "TwoPointDrawElem.h"
#include "TrajectoryBuilder.h"

#define UM_SHOW_VALUE	(WM_USER + 1)
#define UM_SELECTION	(WM_USER + 2)


//typedef CRefCountedAdapter<CBitmap> CBitmap_adpr;
//typedef CRefCounted<CBitmap_adpr> CBitmap_ptr;

typedef boost::shared_ptr<CBitmap> CBitmapPtr;


class CDrawer : public CStatic, public IDrawInterface
{
public:
	enum EDrawType
	{
		eOnePointDistHeight,
		eTwoPoint,
		eOnePointDistSize,
	};
public:
	CDrawer(void);
	~CDrawer(void);
	bool LoadGridParam ( CGridParam *pGp );
	bool AppendApproaches(ListPtrApproachs &listApprc);
	bool AppendApproachHolderInterface(IApproachHolder *pApprchHolderInterface);

	bool SetDrawType(EDrawType etype);
	EDrawType GetDrawType() const;
public:
	DECLARE_MESSAGE_MAP()
	afx_msg void OnPaint();
	afx_msg void OnSize(UINT nType, int cx, int cy);
	afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
	afx_msg void OnMouseMove(UINT nFlags, CPoint point);
	afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
protected:
	CBitmapPtr	GetBitmap(int nWidth, int nHeight);
	CBitmapPtr	GetBitmapForDraw();

	bool DrawApproachs(CDC &cdc, int nWidth, int nHeight, const ListPtrApproachs& traceApprch);
	bool DrawGrid(CDC &cdc, int nWidth, int nHeight);
	bool ReDrawAll();
	bool ReqRedraw(DWORD dwmsec);
	bool DrawCurApproachs(const ListPtrApproachs &listApprc);

	bool SearchNearestApp(CPoint point);
	bool ShowToolTip(CPoint point, LPCTSTR stzText);
	bool HideToolTip();

	static COLORREF GetColorByDist(double dist);
	static COLORREF GetColorByDate(double date);

	unsigned static __stdcall DrawThreadEntryPoint(void * pArg);
	unsigned DrawThreadFn();
protected:
	CCriticalSection	m_csBM;
	CCriticalSection	m_csAppList;
	CBitmapPtr			m_phDrawingBitmap;
	CGridParam			m_GridParam;
	CFont				m_font;

	CEvent				m_evtTerminate;

	CEvent				m_evntAppendTrace;
	CEvent				m_evntRedraw;
	DWORD				m_dwReqRedrawDelay;

	HANDLE				m_hDrawThread;

	ListPtrApproachs		m_listApprchs;
	IApproachHolder *	m_pApprchHolderInterface;

	auto_ptr<CWnd>		m_pWndToolTip;
	bool				m_bToolTipShown;	
	CBrush				m_brushToolTipBkgd;
	CPoint				m_prevPoint;
	CPoint				m_pointBeginSelection;
	
	CLine				m_lineMouse;
	MSelectRect			m_selectedRect;
	EDrawType			m_eDrawType;
};
